<template>
  <div>
    <el-row style="margin-bottom:20px;display:flex;justify-content:flex-start">
      <el-input placeholder="设备名称" v-model="searchmaintain.ep_ename" style="width:20%;margin-right:10px" clearable></el-input>

      <el-input placeholder="规格型号" v-model="searchmaintain.scale" style="width:20%;margin-right:10px" clearable></el-input>
      <el-input placeholder="管理编号" v-model="searchmaintain.managenum" style="width:20%;margin-right:10px" clearable></el-input>
      <el-date-picker
        v-model="repairdate"
        clearable
        style="margin-right:15px;width:20%"
        type="daterange"
        rangeSeparator="至"
        startPlaceholder="维护时间"
        endPlaceholder="维护时间"
        valueFormat="yyyy-MM-dd"
      >
      </el-date-picker>
      <el-input placeholder="维护内容" v-model="searchmaintain.repair_content" style="width:20%;margin-right:10px" clearable></el-input>
      <el-input placeholder="维护人" v-model="searchmaintain.whrname" style="width:20%;margin-right:10px" clearable></el-input>

      <el-button size="mini" type="primary" @click="geteqmaintainList">搜索</el-button>
    </el-row>
    <el-row>
      <el-button size="mini" type="primary" @click="donewmaintain">新增维护记录</el-button>
      <el-button size="mini" type="primary" @click="editmaintain">编辑</el-button>
      <el-button size="mini" type="success" @click="deletemaintain">删除</el-button>
    </el-row>
    <el-divider></el-divider>
    <maintainList :eqmaintainList="eqmaintainList" :choosedp.sync="choosedp" :pagobj.sync="pagobj" :ismanager="false"></maintainList>
    <neweqmaintain v-if="isneweqmaintain" :editdata="editdata" :isedit="isedit"></neweqmaintain>
  </div>
</template>

<script>
import maintainList from '~/components/apparatus/eqmaintain/maintainList.vue';
import neweqmaintain from '~/components/apparatus/eqmaintain/neweqmaintain.vue';

export default {
  components: {
    maintainList,
    neweqmaintain,
  },
  data() {
    return {
      ts: this.newStore(),
      searchmaintain: {},
      isedit: false,
      repairdate: ['', ''],
      eqmaintainList: [],
      choosedp: [],
      editdata: [],
      isneweqmaintain: false,
      pagobj: {
        currentPage3: 1,
        pagesize: 10,
        total: 10,
      },
    };
  },
  created() {
    this.geteqmaintainList();
  },
  methods: {
    donewmaintain() {
      this.isedit = false;
      this.isneweqmaintain = false;
      this.$nextTick(() => {
        this.isneweqmaintain = true;
      });
    },
    editmaintain() {
      if (this.choosedp.length == 0) {
        this.$message({
          type: 'info',
          message: '请先勾选维护记录',
        });
        return;
      }
      if (this.choosedp.length > 1) {
        this.$message({
          type: 'info',
          message: '只能编辑一条维护记录',
        });
        return;
      }
      this.isedit = true;
      const req = {
        handle: 'DD5D108B99FC12B735E83B084D0B700869B8AC1FA85E149EE7E2EF52E03FE1661662A39FC030365F947C61655EDF54FA',
        rid: this.choosedp[0].hash.rid,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.editdata = ts.d.data;
        this.isneweqmaintain = false;
        this.$nextTick(() => {
          this.isneweqmaintain = true;
        });
      });
    },
    deletemaintain() {
      if (this.choosedp.length == 0) {
        this.$message({
          type: 'info',
          message: '请先勾选维护记录',
        });
        return;
      }
      const deletearr = [];
      for (let i = 0; i < this.choosedp.length; i += 1) {
        deletearr.push(this.choosedp[i].hash.rid);
      }

      this.$confirm('确认删除已选择的维护记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const req = {
            handle:
              'DD5D108B99FC12B735E83B084D0B700869B8AC1FA85E149EEAA0F765F26818C2B3A0C37CD1BE2AD4BFF0B9210751F692',
            rid: deletearr,
          };
          this.spost(this.ts, '/mainservlet', req).then(() => {
            this.$message({
              type: 'success',
              message: '删除成功',
            });
            this.geteqmaintainList();
          });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          });
        });
    },
    geteqmaintainList() {
      if (this.repairdate == null) {
        this.repairdate = [];
      }
      const req = this.searchmaintain;
      req.handle = 'DD5D108B99FC12B735E83B084D0B700869B8AC1FA85E149EB0C68447069A54E9888636941D7FA35B';
      req.fzrid = localStorage.getItem('pid');
      [req.repair_time_begin] = [this.repairdate[0]];
      [req.repair_time_end] = [this.repairdate[1]];
      req.start = (this.pagobj.currentPage3 - 1) * this.pagobj.pagesize;
      req.limit = this.pagobj.pagesize;
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.eqmaintainList = ts.d.data.operation1;
        this.pagobj.total = parseInt(ts.d.data.operation2[0].hash.allrs);
      });
    },
  },
};
</script>
<style >

</style>

